package com.relsell.view.phone;

import java.util.ArrayList;
import java.util.List;

import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

import com.relsell.controller.CustomListViewAdapter;
import com.relsell.controller.DataController;
import com.relsell.controller.DataControllerInterface;
import com.relsell.controller.DataInterface;
import com.relsell.controller.InstructorCallBack;
import com.relsell.model.InstructorsDataModel;
import com.relsell.model.RowItem;
import com.relsell.tmcprototype.R;
import com.relsell.utility.ApplicationVars;

public class Instructors extends FragmentActivity implements
		OnItemClickListener, InstructorCallBack {

	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		super.onConfigurationChanged(newConfig);
		inflateView();
	}

	public static final int GOTDATAFORINSTRUCTORS = 23;

	public static final String[] titles = new String[] { "Name1", "Name2",
			"Name3", "Name4" };

	public static final String[] descriptions = new String[] { "description1",
			"description2", "description3", "description4" };

	public static final Integer[] images = { R.drawable.instructors_icon,
			R.drawable.instructors_icon, R.drawable.instructors_icon,
			R.drawable.instructors_icon };

	ListView listView;
	List<RowItem> rowItems;

	String icon, desc, styledesc, url;
	DataControllerInterface mdcIObj;

	private LinearLayout mRestLinearLayout;

	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		setContentView(R.layout.instructors);
		mRestLinearLayout = (LinearLayout) findViewById(R.id.restLinearLayout);
		listView = (ListView) findViewById(R.id.list);
		ApplicationVars.getInstance().setEmptyInstructorCallBack(this);
		if (ApplicationVars.getInstance().getInstructorsDataList() == null) {
			mdcIObj = new DataController();
			mdcIObj.startProcess(1, this);
		} else {
			inflateView();
		}

	}

	public void inflateView() {
		if (rowItems == null) {
			rowItems = new ArrayList<RowItem>();
		}

		ApplicationVars localObj = ApplicationVars.getInstance();
		ArrayList<DataInterface> list = localObj.getInstructorsDataList();

		DataInterface dl = null;
		RowItem rowItem = null;
		for (int i = 0; i < list.size(); i++) {

			dl = list.get(i);
			if (dl instanceof InstructorsDataModel) {
				String id = ((InstructorsDataModel) dl).getId();
				String name = ((InstructorsDataModel) dl).getName();
				rowItem = new RowItem(R.drawable.instructors_icon, id, name);
				rowItems.add(rowItem);
			}
		}

		mRestLinearLayout.setVisibility(View.GONE);
		CustomListViewAdapter adapter = new CustomListViewAdapter(this,
				R.layout.list_item, rowItems);
		listView.setAdapter(adapter);
		listView.setOnItemClickListener(this);
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		// Toast toast = Toast.makeText(getApplicationContext(), "Item "
		// + (position + 1) + ": " + rowItems.get(position),
		// Toast.LENGTH_SHORT);
		// toast.setGravity(Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
		// toast.show();
	}

	@Override
	public void gotInstructorListData(boolean result) {
		inflateView();
	}

}
